const app = getApp()
Page({
  data: {
    serverTypes: ["上门服务", "送修到店", "远程协助", "电话指导"],
    serverTypesIndex: 0,
    buyTypes: ["在线支付", "现金付款", "工程师代收"],
    buyTypesIndex: 0,
    accessories_price: '',
    service_price: '',
    discount_price: '',
    price:0
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    app.getParam(options, (rel) => {
      this.setData({
        id: rel.id,
      })
    })
  },
  onShow: function () {
    app.api().Get('api/v1/bestbao/engineer/reView',{id: this.data.id},(rel)=>{
      console.log('确认订单');
    })
  },
  //金额限制
  pointNum: function (obj) {
    obj = obj.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj = obj.replace(/^\./g, ""); //验证第一个字符是数字
    obj = obj.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj = obj.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    return obj;
  },
  //配件费用
  accessories_price: function (e) {
    this.setData({
      accessories_price: '' == e.detail.value ? '' : this.pointNum(e.detail.value)
    })
    this.price();
  },
  //工单费用
  service_price: function (e) {
    this.setData({
      service_price: '' == e.detail.value ? '' : this.pointNum(e.detail.value)
    })
    this.price();
  },
  //优惠费用
  discount_price: function (e) {
    this.setData({
      discount_price: '' == e.detail.value ? '' : this.pointNum(e.detail.value)
    })
    this.price();
  },
  //计算应付基恩
  price:function () {
    var accessories_price = this.data.accessories_price== '' ? 0 :parseFloat(this.data.accessories_price),
      service_price = this.data.service_price == '' ? 0 :parseFloat(this.data.service_price),
      discount_price = this.data.discount_price == '' ? 0 :parseFloat(this.data.discount_price),
      price = accessories_price + service_price - discount_price;
    this.setData({
      price: price.toFixed(2)
    })
  },
  //服务方式
  bindServerChange: function (e) {
    this.setData({
      serverTypesIndex: e.detail.value
    })
  },
  //支付方式
  bindBuyChange: function (e) {
    this.setData({
      buyTypesIndex: e.detail.value
    })
  },
  //表单提交
  formSubmit: function (e) {
    var data = e.detail.value;
    if (app.util().isNull(data.question)){
      app.wxAlert('实际问题原因必须填写');
      return false;
    } else if (app.util().isNull(data.answer)){
      app.wxAlert('你的解决方案必须填写');
      return false;
    } else if (app.util().isNull(data.accessories_price)) {
      app.wxAlert('配件费用必须填写,没有填写 0 元');
      return false;
    } else if (app.util().isNull(data.service_price)) {
      app.wxAlert('工单费用必须填写,没有填写 0 元');
      return false;
    }else{
      if (this.data.price < 0){
        app.wxAlert('应付金额不能小于0元');
        return false;
      }
    }
    wx.showLoading({
      title: '提交中',
    })
    var param = {
      servertypes: this.data.serverTypes[this.data.serverTypesIndex],
      buytypes: this.data.buyTypes[this.data.buyTypesIndex],
      question: data.question,
      answer: data.answer,
      accessories: data.accessories,
      accessories_price: this.data.accessories_price == '' ? 0 : this.data.accessories_price,
      service_price: this.data.service_price == '' ? 0 : this.data.service_price,
      discount_price: this.data.discount_price == '' ? 0 : this.data.discount_price,
      price: this.data.price,
      id: this.data.id,
    }
    app.api().Post('api/v1/bestbao/engineer/orderEnd', param, (result) => {
      if (result.code == 200) {
        app.wxAlert(result.msg,() => {
          wx.navigateBack()
        })
      }
      wx.hideLoading()
    })
    console.log(data);
  },
});